package edu.taru.view;

import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import edu.taru.pojo.User;
import edu.taru.service.*;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.security.Provider.Service;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;

public class LoginView extends JFrame{
	private JTextField textField;
	private JPasswordField passwordField;
	List<User> list = new ArrayList<User>();
	public LoginView() {
		setTitle("人力资源管理系统");
		setSize(485,342);
		setVisible(true);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setBackground(Color.WHITE);
		getContentPane().setLayout(null);
		
		JLabel label = new JLabel("用户名");
		label.setFont(new Font("微软雅黑", Font.BOLD, 14));
		label.setBounds(38, 223, 54, 15);
		getContentPane().add(label);
		
		textField = new JTextField();
		textField.setBounds(102, 221, 219, 21);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel label_1 = new JLabel("密   码");
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
		label_1.setBounds(38, 257, 54, 15);
		getContentPane().add(label_1);
		/**
		 * 登录
		 */
		JButton button = new JButton("登录");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取用户名
				String username = textField.getText();
				//获取密码
				String password = new String(passwordField.getPassword());
				try {
					Services service = new Services();
					User user = service.login(username,password);
					if(user!=null) {
					MainView mainView = new MainView();
					list.add(user);
					LoginView.this.dispose();
				}else {
					JOptionPane.showMessageDialog(LoginView.this, "用户名或者密码错误");
				}
			}catch(Exception ex) {
				JOptionPane.showMessageDialog(LoginView.this, "出现异常，请联系管理员1848992877@qq.com");
			}
				
				
			}
		});
		button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		button.setBounds(341, 220, 93, 23);
		getContentPane().add(button);
		/**
		 * 退出系统
		 */
		JButton button_1 = new JButton("退出");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoginView.this.dispose();
			}
		});
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		button_1.setBounds(341, 254, 93, 23);
		getContentPane().add(button_1);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon(LoginView.class.getResource("/images/26W58PICj6f.jpg")));
		lblNewLabel.setBounds(0, 0, 469, 211);
		getContentPane().add(lblNewLabel);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(102, 255, 219, 21);
		getContentPane().add(passwordField);
	}
}
